﻿using EntityFrameworkDemo.Models;
using System;
using System.Data.Entity;
using System.Linq;

namespace EntityFrameworkDemo
{

    class Program
    {

        static void Main(string[] args)
        {
            Database.SetInitializer(new ApplicationDbInitializer());

            Console.WriteLine("Welcome to the Entity Framework Demo!");

            // If you have a SQL Server instance installed locally, use the non-param constructor.
            // The connection string is written in the App.config file.
            // using (var db = new EntityFrameworkDemoDbContext())
            // Otherwise, use the SQL Server Compact 4.0 as the database.
            using (var db = new ApplicationDbContext(ApplicationDbContext.SqlCeConnectionString))
            {
                //                if (!db.Database.Exists())
                //                {
                //                    Console.WriteLine("Creating Database...");
                //                    db.Database.Create();
                //                    db.SaveChanges();
                //                }
                //#if DEBUG
                //                else
                //                {
                //                    db.Database.Delete();
                //                    db.SaveChanges();
                //                }
                //#endif
                while (true)
                {
                    Console.WriteLine("1-Add records;\n2-Search for a record;\n3-Delete a record;\n4-List all records;\n0-Exit;\nPlease choose a function:");
                    var i = int.Parse(Console.ReadLine());
                    switch (i)
                    {
                        case 1:
                            AddRecord(db);
                            break;

                        case 2:
                            FindByName(db);
                            break;

                        case 3:
                            DeleteById(db);
                            break;

                        case 4:
                            ListRecords(db);
                            break;

                        case 0:
                            Console.WriteLine("Press any key to continue.");
                            Console.ReadKey(false);
                            return;

                        default:
                            Console.WriteLine("Incorrect input! Please retry.");
                            break;
                    }
                }
            }
        }

        public static void AddRecord(ApplicationDbContext db)
        {
            while (true)
            {
                Console.WriteLine("Please input name: ");
                var name = Console.ReadLine();
                Console.WriteLine("Please input date: ");
                var date = Console.ReadLine();
                Console.WriteLine("Please input if he or she will attend: ");
                var n = Console.ReadLine();
                var item = new RecordModel();
                try
                {
                    item.Name = name;
                    item.Date = DateTime.Parse(date);
                    item.IsAttend = bool.Parse(n);
                    if (db.Records.Any(x => x.Name == item.Name && x.Date == item.Date))
                    {
                        Console.WriteLine("There is a record with the same name and date in the database. Please try another record.");
                        continue;
                    }
                    db.Records.Add(item);
                    db.SaveChanges();
                    return;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        public static void FindByName(ApplicationDbContext db)
        {
            Console.WriteLine("Please input the name to search: ");
            var name = Console.ReadLine();
            var flag = false;
            foreach (var item in db.Records)
            {
                if (item.Name.Equals(name))
                {
                    Console.WriteLine("Id:{0},name:{1},date:{2},attend:{3}", item.Id, item.Name, item.Date, item.IsAttend.ToString());
                    flag = true;
                    break;
                }
            }
            if (flag == false)
            {
                Console.WriteLine("No record found.");
            }
        }

        public static void DeleteById(ApplicationDbContext db)
        {
            while (true)
            {
                try
                {
                    Console.WriteLine("Please input the id to delete: ");
#if USE_GUID
                    var x = Guid.Parse(Console.ReadLine());
#else
                    var x = int.Parse(Console.ReadLine());
#endif
                    foreach (var item in db.Records)
                    {
                        if (item.Id == x)
                        {
                            db.Records.Remove(item);
                            Console.WriteLine("Record {0} has been deleted.", x);
                            return;
                        }
                    }
                    Console.WriteLine("No record found.");
                    return;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    continue;
                }
            }
        }

        public static void ListRecords(ApplicationDbContext db)
        {
            Console.WriteLine("Id\tName\tDate\tAttend");
            foreach (var item in db.Records)
            {
                Console.WriteLine(item.ToString());
            }
        }

    }

}